//
//  Home_ViewController.m
//  International_Hospital
//
//  Created by LiYiLin on 16/7/4.
//  Copyright © 2016年 LiYiLin. All rights reserved.
//

#import "Home_ViewController.h"
#import "WeatherView.h"
#import "WeatherView_Model.h"
#import "LoopView_CollectionViewCell.h"

@interface Home_ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, weak) WeatherView * weather_View;

@property (nonatomic, weak) UICollectionView * loopCollectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout * flowLayout;

@property (nonatomic, weak) UIView * famous_View;
@property (nonatomic, weak) UIView * sixDivView;

//装有轮播器数据
@property (nonatomic, strong) NSArray * array;
//轮播器的计时器
@property (nonatomic, strong) NSTimer * timer;

@end

@implementation Home_ViewController

- (void)setArray:(NSArray *)array {
    _array = array;
    [self.loopCollectionView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setup_UI];
    [self setup_Weather];
    [self setup_loopView];
    [self setup_Famous];
    [self setup_SixDiv];
    [self setup_SixDivContent];
    [self addTimer];
}

- (void)setup_UI {
    
    //DefineConstant.h 中宏定义的主题颜色 navgationBar的背景色
    [self.navigationController.navigationBar setBarTintColor:kThemeColor];
    
    UIImage * image = [UIImage imageNamed:@"l"];
    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem.image = image;
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    [self setTitle:@"健康生活每一天"];
}

#pragma mark - 天气部分
- (void)setup_Weather {
    
    WeatherView * weather_View = [[WeatherView alloc] init];
    [self.view addSubview:weather_View];
    self.weather_View = weather_View;
    [self.weather_View mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).offset(64);
        make.height.mas_equalTo(80);
        make.left.equalTo(self.view.mas_left).offset(0);
        make.right.equalTo(self.view.mas_right).offset(0);
    }];
    weather_View.backgroundColor = LIGHTBLUE_COLOR;
    [WeatherView_Model weatherWithCurrentCity:@"上海" finishBlock:^(WeatherView_Model *weatherModel) {
        
        weather_View.weatherModel = weatherModel;
        
    } errorBlock:^{
        
    }];
}

#pragma mark - 轮播器部分
- (void)setup_loopView{

    UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 0;
    self.flowLayout = flowLayout;
    
    UICollectionViewController * vc = [[UICollectionViewController alloc] initWithCollectionViewLayout:flowLayout];
    self.loopCollectionView = vc.collectionView;
    
    [self.view addSubview:vc.collectionView];
    [vc.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.weather_View.mas_bottom).offset(0);
        make.height.mas_equalTo(200);
        make.left.mas_equalTo(self.view.mas_left).offset(0);
        make.right.mas_equalTo(self.view.mas_right).offset(0);
    }];
    self.loopCollectionView.dataSource =self;
    self.loopCollectionView.delegate = self;
    
    [LoopView_Model loopView_ModelWithSuccess:^(NSArray *loopView_Model) {
        self.array = loopView_Model;
    } error:^{
        NSLog(@"Error!");
    }];
    
    [self.loopCollectionView registerClass:[LoopView_CollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    
}

-(void) addTimer{
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
    self.timer = timer ;
    
}


-(void) removeTimer{
    [self.timer invalidate];
    self.timer = nil;
}

-(void) scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [self removeTimer];
}


-(void) scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    [self addTimer];
    
}

-(void) nextPage{
    NSIndexPath *currentIndexPath = [[self.loopCollectionView indexPathsForVisibleItems] lastObject];
    
    NSIndexPath *currentIndexPathReset = [NSIndexPath indexPathForItem:currentIndexPath.item inSection:3/2];
    [self.loopCollectionView scrollToItemAtIndexPath:currentIndexPathReset atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
    
    NSInteger nextItem = currentIndexPathReset.item +1;
    NSInteger nextSection = currentIndexPathReset.section;
    if (nextItem==4) {
        nextItem=0;
        nextSection++;
    }
    NSIndexPath *nextIndexPath = [NSIndexPath indexPathForItem:nextItem inSection:nextSection];
    [self.loopCollectionView scrollToItemAtIndexPath:nextIndexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
}

- (void)viewDidLayoutSubviews {
    [self update_SixDivContent];
    self.flowLayout.itemSize = self.loopCollectionView.frame.size;
    self.flowLayout.minimumInteritemSpacing = 0;
    self.flowLayout.minimumLineSpacing = 0;
    self.flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.loopCollectionView.pagingEnabled = YES;
    self.loopCollectionView.bounces = NO;
}

#pragma mark <UICollectionViewDataSource>
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return 3;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.array.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    LoopView_CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    LoopView_Model * model = self.array[indexPath.item];
    cell.tag = indexPath.item;
    cell.model = model;    
    return cell;
}


#pragma mark - 名医通部分
- (void)setup_Famous {
    
    UIView * famousView = [[UIView alloc] init];
    self.famous_View = famousView;
    
    [self.view addSubview:famousView];
    [famousView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.loopCollectionView.mas_bottom).offset(0);
        make.height.mas_equalTo(50);
        make.left.mas_equalTo(self.view.mas_left).offset(0);
        make.right.mas_equalTo(self.view.mas_right).offset(0);
    }];
    famousView.backgroundColor = [UIColor grayColor];
}

#pragma mark - 主页六个模块
- (void)setup_SixDiv {
    
    UIView * SixDivView = [[UIView alloc] init];
    self.sixDivView = SixDivView;
    
    [self.view addSubview:SixDivView];
    [SixDivView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.famous_View).offset(0);
        make.bottom.mas_equalTo(self.view.mas_bottom).offset(0);
        make.left.mas_equalTo(self.view.mas_left).offset(0);
        make.right.mas_equalTo(self.view.mas_right).offset(0);
    }];
    [SixDivView setBackgroundColor:[UIColor lightGrayColor]];
}

#pragma mark - 六个模块的内容
- (void)setup_SixDivContent {
    
    NSArray * arrayImg = @[@"zl",@"xyg",@"sj",@"xy",@"gt",@"gy"];
    NSArray * arrayLabel = @[@"肿瘤",@"心血管",@"神经科",@"血液科",@"骨科",@"公益"];
    
    for (NSInteger i = 0; i < 6; i++) {
    
        UIView * iView = [[UIView alloc] init];
        iView.backgroundColor = [UIColor whiteColor];
        
        UILabel *textLabel = [[UILabel alloc] init];
        textLabel.text = arrayLabel[i];
        [textLabel sizeToFit];
        UIImageView * imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:arrayImg[i]]];
        [imageView sizeToFit];
        
        [iView addSubview:textLabel];
        [iView addSubview:imageView];
        
        [self.sixDivView addSubview:iView];
    }
}

- (void)update_SixDivContent {
    
    for (NSInteger i = 0; i < 6; i++) {
        NSInteger row =  i / 3;
        
        CGFloat margin = 2;
        CGFloat width  = (self.sixDivView.bounds.size.width - 4 * margin ) / 3;
        CGFloat height = (self.sixDivView.bounds.size.height - 4 * margin) / 2;
        CGFloat x = width * ( i % 3) + margin * (i % 3) + margin;
        CGFloat y = row * height + margin * row + margin;
        
        CGRect viewFrame = CGRectMake(x, y, width, height);
        
        UIView * iView = self.sixDivView.subviews[i];
        iView.frame = viewFrame;
        iView.backgroundColor = [UIColor whiteColor];
        
        UILabel *textLabel = (UILabel *)iView.subviews[0];

        UIImageView * imageView = (UIImageView *)iView.subviews[1];

        CGFloat labelHeight = textLabel.bounds.size.height;
        CGFloat imgHeight = imageView.bounds.size.height;
        CGFloat imgY = (iView.frame.size.height - imgHeight - labelHeight) / 2 + imgHeight / 2;
        imageView.center = CGPointMake(iView.frame.size.width / 2, imgY);
        
        CGFloat labelY = CGRectGetMaxY(imageView.frame) + labelHeight / 2 + 5;
        textLabel.center = CGPointMake(iView.frame.size.width / 2, labelY);
    }
}


@end















